import json

# json字符串转python
json_str = """{
  "sites": {
    "site": [
      {
        "id": "1",
        "name": "菜鸟教程",
        "url": "www.runoob.com"
      },
      {
        "id": "2",
        "name": "菜鸟工具",
        "url": "c.runoob.com"
      },
      {
        "id": "3",
        "name": "Google",
        "url": "www.google.com"
      }
    ]
  }
}"""

rs = json.loads(json_str)
print(rs)
print(type(rs))

# json文件转python
with open('jsonTest.json', encoding='utf8') as fp:
    python = json.load(fp)
    print(python)

# python转json字符串
with open('jsonTest.json', encoding='utf8') as fp:
    python = json.load(fp)
    json_str1 = json.dumps(python, ensure_ascii=False)
    print(json_str1)
    # python写入json文件
    with open('jsonTest2.json','w', encoding='utf8') as fp2:
        json.dump(python, fp2,ensure_ascii=False)
